﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GONES.IBLL;
using GONES.IDAL;
using GONES.Model;
using log4net;
using System.Reflection;
using Newtonsoft.Json;
using GONES.Tools.Common;

namespace GONES.BLL
{
    public class S_UserRoleManager : IS_UserRoleManager
    {
        protected static readonly ILog Log =
                LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private readonly IUserInfo _userInfo;
        public IS_UserRoleDao S_UserRoleDao;
        public IS_LogDao S_LogDao;
        public S_LogManager S_LogManager;
        public string ErrMsg { get; set; }

        public S_UserRoleManager(IUserInfo userinfo)
        {
            _userInfo = userinfo;
        }

        public void Add(S_UserRole userrole)
        {
            if (IsError(userrole))
                return;
            userrole.CreateTime = System.DateTime.Now;
            S_UserRoleDao.Save(userrole);
            SaveLog("用户角色管理", "用户角色添加", "系统管理", userrole);
        }

        public void Edit(S_UserRole userrole)
        {
            if (IsError(userrole))
                return;
            userrole.ModifyTime = System.DateTime.Now;
            S_UserRoleDao.Update(userrole);
            SaveLog("用户角色管理", "用户角色修改", "系统管理", userrole);
        }

     

        public IList<S_UserRole> GetUserRolesList(string uid)
        {
            return S_UserRoleDao.GetHqlList(string.Format("select e from S_UserRole e where Uid='{0}'", uid));
        }


        public void Delete(string id)
        {
            S_UserRole userrole = S_UserRoleDao.Get(id);
            S_UserRoleDao.Delete(userrole);
            SaveLog("用户角色管理", "用户角色删除", "系统管理", userrole);
        }

        private void SaveLog(string module, string operate, string logtype, S_UserRole userrole)
        {
            S_LogManager = new S_LogManager(_userInfo);
            string detail = string.Format("{0}:用户编号是{1}，角色编号是{2}", operate, userrole.Uid, userrole.RoleId);
            SaveLog(module, operate, logtype, detail);
            if (Log.IsInfoEnabled)
                Log.Info(string.Format("{0}：{1}", operate, JsonConvert.SerializeObject(userrole)));
        }

        protected bool IsError(S_UserRole userrole)
        {
            if (string.IsNullOrEmpty(userrole.Uid))
            {
                ErrMsg = "用户编号不能为空！";
            }
            if (string.IsNullOrEmpty(userrole.RoleId))
            {
                ErrMsg = "角色编号不能为空！";
            }
            if (!string.IsNullOrEmpty(ErrMsg))
                return true;
            return false;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="operate">操作名称</param>
        /// <param name="logtype">日志类型（系统管理、基础资料等）</param>
        /// <param name="detail">详细日志</param>
        protected void SaveLog(string module, string operate, string logtype, string detail)
        {
            try
            {
                S_LogDao.Save(new S_Log
                {
                    ID = Rnd.Guid.ToString(),
                    Module = module,
                    Operate = operate,
                    Detail = detail,
                    LogType = logtype,
                    OpDate = DateTime.Now,
                    CreateUser = _userInfo.CLoginInfo.UserName,
                    CreateTime = DateTime.Now,
                    ModifyUser = _userInfo.CLoginInfo.UserName,
                    ModifyTime = DateTime.Now,
                    Ip = Tools.Common.Tools.GetIP()
                });
            }
            catch
            {
            }
        }
    }
}
